/*
Copyright 2009 Johan Maasing, Mikael Forsberg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.google.code.pj2r.util.resource;

import com.google.code.pj2r.DataAccessException;
import com.google.code.pj2r.MarshallingException;
import com.google.code.pj2r.Resource;
import com.google.code.pj2r.ResourceException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Utility parent class for resources that want to eturn JSONObject or
 * JSONArray as result instead of strings.
 * @author joma7188
 */
public abstract class JSONResource implements Resource {

    public abstract String create() throws DataAccessException;

    /**
     * Subclasses should not override this method. This method converts
     * a JSONObject to a string. The JSONObject is created in the
     * {@link #getObject(java.lang.String)} method.
     * @see #getObject(java.lang.String)
     * @param id
     * @return
     * @throws ResourceException
     * @throws JSONException
     */
    public String get(String id) throws DataAccessException, MarshallingException {
        try {
            JSONObject result = getObject(id);
            if (result != null) {
                return result.toString(2);
            }
        } catch (JSONException ex) {
            Logger.getLogger(JSONResource.class.getName()).log(Level.SEVERE, null, ex);
            throw new MarshallingException(ex);
        }
        return null;
    }

    /**
     * Subclasses should not override this method. This method converts an
     * JSONArray to a string. The JSONArray is creatd in the {@link #listArray() } method.
     * @return
     * @throws ResourceException
     * @throws MarshallingException
     */
    public String list() throws DataAccessException, MarshallingException {
        try {
            JSONArray array = listArray();
            if (array != null) {
                return array.toString(2);
            }
        } catch (JSONException ex) {
            Logger.getLogger(JSONResource.class.getName()).log(Level.SEVERE, null, ex);
            throw new MarshallingException(ex);
        }
        return null;
    }

    /**
     * Subclasses should override this method to produce a JSONObject representation
     * of the resource. This the object version of {@link Resource#get(java.lang.String)}
     * @see Resource#get(java.lang.String)
     * @param id
     * @return
     * @throws ResourceException
     * @throws JSONException
     */
    protected abstract JSONObject getObject(String id) throws DataAccessException, JSONException;

    /**
     * Subclasses should override this methif to produce a JSONArray representation
     * of the resources. This is the object version of {@link Resource#list() }
     * @see Resource#list()
     * @return
     * @throws ResourceException
     * @throws JSONException
     */
    protected abstract JSONArray listArray() throws DataAccessException, JSONException;
}
